//
//  TMHomeSubVC.m
//  TMBD Movie App
//
//  Created by zhangkai on 2023/4/2.
//

#import "TMHomeSubVC.h"
#import <Masonry.h>
#import "TMHomeSubVCCell.h"
#import "TMTVInfoVC.h"
#import "HomeModel.h"
#import "TMHomeSubBannView.h"
#import "SDCycleScrollView.h"
#import "TMDetailsVc.h"
#import "TMCollectionReusableView.h"
@interface TMHomeSubVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic)UICollectionView *mainCollectionView;
@property(strong,nonatomic)SDCycleScrollView *mainScro;
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

@property(nonatomic,strong)NSArray *bannAr;
@property(nonatomic,strong)NSArray *listArr;
@end

@implementation TMHomeSubVC
- (void)getTVData{
    
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"json"];
    // 将JSON文件转换为NSData
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    // 解析JSON数据
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"JSON 解析错误：%@", error.localizedDescription);
    } else {
        NSLog(@"JSON 数据：%@", json);
    }
    
    NSMutableArray *arr = @[].mutableCopy;
    NSMutableArray *arr1 = @[].mutableCopy;
    
    HomeTVModel *model = [HomeTVModel mj_objectWithKeyValues:json];
    if([model.allDic isKindOfClass:[NSArray class]]){
        NSArray *newArr = (NSArray *)model.allDic;
        for (NSDictionary *tvDic in newArr) {
            
            if([tvDic[@"134"] isEqualToString:@"banner"]){
                [arr addObject:tvDic];
            }else{
                [arr1 addObject:tvDic];
            }
            
            
            
        }
        
        
    }
    
    
    self.listArr = arr1.copy;
    self.bannAr = arr.copy;
    [self.mainCollectionView reloadData];
    
    NSMutableArray *arrpic = @[].mutableCopy;
    for (NSDictionary *pics in self.bannAr) {
        
        NSArray *arr = [pics objectForKey:@"264"];
        for (NSDictionary *pic in arr) {
            [arrpic addObject:pic[@"img"]];
        }
        
        
    }
    
    
    self.mainScro.imageURLStringsGroup = arrpic;
    
    
    
}
- (void)getData{
    
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"data_list" ofType:@"json"];
    // 将JSON文件转换为NSData
    NSData *data = [NSData dataWithContentsOfFile:jsonPath];
    // 解析JSON数据
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    if (error) {
        NSLog(@"JSON 解析错误：%@", error.localizedDescription);
    } else {
        NSLog(@"JSON 数据：%@", json);
    }
    
    HomeModel *model = [HomeModel mj_objectWithKeyValues:json];
    
    NSArray *modelArr = [HomeoneModel mj_objectArrayWithKeyValuesArray:model.allDic[@"79"][@"264"]];
    
    NSMutableArray *arr = @[].mutableCopy;
    NSMutableArray *arr1 = @[].mutableCopy;
    for (HomeoneModel *tempModel in modelArr) {
        if([tempModel.title isEqualToString:@"banner"]){
            [arr addObject:tempModel];
            
        }else{
            [arr1 addObject:tempModel];
        }
    }
    
    self.listArr = arr1.copy;
    self.bannAr = arr.copy;
    [self.mainCollectionView reloadData];
    
    NSMutableArray *arrpic = @[].mutableCopy;
    if(arr.count > 0){
        HomeoneModel *picModel = arr[0];
        for (NSDictionary *picDic in picModel.allDic) {
            [arrpic addObject:picDic[@"img"]];
        }
    }
    
    self.mainScro.imageURLStringsGroup = arrpic;
    
    
    
    
    
    
    
}



- (void)getInfo{
    
    
    NSDictionary *dic = @{
        @"page":@"1",
        @"page_size":@"12",
        @"reg_id":@" ",
    };
    //
    
    [[TMNetworkManage defaultManager] MVWithApiName:@"250/" parameters:dic success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        
        HomeModel *model = [HomeModel mj_objectWithKeyValues:responseObject];
        
        NSArray *modelArr = [HomeoneModel mj_objectArrayWithKeyValuesArray:model.allDic[@"79"][@"264"]];
        
        NSMutableArray *arr = @[].mutableCopy;
        NSMutableArray *arr1 = @[].mutableCopy;
        for (HomeoneModel *tempModel in modelArr) {
            if([tempModel.title isEqualToString:@"banner"]){
                [arr addObject:tempModel];
                
            }else{
                [arr1 addObject:tempModel];
            }
        }
        
        self.listArr = arr1.copy;
        self.bannAr = arr.copy;
        [self.mainCollectionView reloadData];
        
        NSMutableArray *arrpic = @[].mutableCopy;
        if(arr.count > 0){
            HomeoneModel *picModel = arr[0];
            for (NSDictionary *picDic in picModel.allDic) {
                [arrpic addObject:picDic[@"img"]];
            }
        }
        
        self.mainScro.imageURLStringsGroup = arrpic;
        
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        
    }];
    
}

- (void)getTVInfo{
    
    NSDictionary *dic = @{
        @"page":@"1",
        @"page_size":@"12",
        @"reg_id":@" ",
    };
    //
    
    [[TMNetworkManage defaultManager] MVWithApiName:@"246/" parameters:dic success:^(NSURLSessionDataTask * _Nonnull dataTask, id  _Nonnull responseObject) {
        
        NSMutableArray *arr = @[].mutableCopy;
        NSMutableArray *arr1 = @[].mutableCopy;
        
        HomeTVModel *model = [HomeTVModel mj_objectWithKeyValues:responseObject];
        if([model.allDic isKindOfClass:[NSArray class]]){
            NSArray *newArr = (NSArray *)model.allDic;
            for (NSDictionary *tvDic in newArr) {
                
                if([tvDic[@"134"] isEqualToString:@"banner"]){
                    [arr addObject:tvDic];
                }else{
                    [arr1 addObject:tvDic];
                }
                
                
                
            }
            
            
        }
        
        
        self.listArr = arr1.copy;
        self.bannAr = arr.copy;
        [self.mainCollectionView reloadData];
        
        NSMutableArray *arrpic = @[].mutableCopy;
        for (NSDictionary *pics in self.bannAr) {
            
            NSArray *arr = [pics objectForKey:@"264"];
            for (NSDictionary *pic in arr) {
                [arrpic addObject:pic[@"img"]];
            }
  
        }

        self.mainScro.imageURLStringsGroup = arrpic;
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull dataTask, NSError * _Nonnull error) {
        
    }];
    
    
}





-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if ([self.type isEqualToString:@"277"]) {
//        [self getData];
        [self getInfo];
    }else{
        [self getTVInfo];
//        [self getTVData];
    }
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.bannAr = @[].copy;
    self.listArr = @[].copy;
    
    UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
    [self.view addSubview:self.mainCollectionView];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    [self.mainCollectionView registerClass:[TMHomeSubVCCell class] forCellWithReuseIdentifier:@"TMHomeSubVCCell"];
    
    [self.mainCollectionView registerClass:[TMCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TMSectionView"];
    [self.mainCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"TMfootView"];
    
    [self.mainCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.mainCollectionView.backgroundColor = [UIColor colorWithRed:13/255.0 green:19/255.0 blue:35/255.0 alpha:1];
    
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(310, 0, 0, 0);
    
    self.mainCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(clickReload)];
    
   
//    TMHomeSubBannView *bannView = [TMHomeSubBannView new];
//    bannView.frame = CGRectMake(0, -310, kScreenW, 310);
//    bannView.backgroundColor = [UIColor yellowColor];
//    [self.mainCollectionView addSubview:bannView];

    
    self.mainScro = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, -310, kScreenW, 310)];
    [self.mainCollectionView addSubview:self.mainScro];
    self.mainScro.bannerImageViewContentMode = UIViewContentModeScaleAspectFill;
    
   
}

- (void)clickReload{
    
    [self.mainCollectionView.mj_header endRefreshing];
    
}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.listArr.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if([self.type isEqualToString:@"277"]){
        HomeoneModel *model = self.listArr[section];
        if(model.allDic.count > 0){
            NSDictionary *dic = model.allDic.firstObject;
            NSArray *arr = [dic objectForKey:self.type];
            return arr.count;
            
        }else{
            return 0;
        }
    }else{
        

        NSDictionary *model = self.listArr[section];
        NSArray *arr = [model objectForKey:@"264"];
        
        if(arr.count > 0){
            NSDictionary *dic = arr.firstObject;
            NSArray *arr = [dic objectForKey:self.type];
            return arr.count;
            
        }else{
            return 0;
        }
    }
    
    

}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    TMHomeSubVCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TMHomeSubVCCell" forIndexPath:indexPath];
//    cell.backgroundColor = [UIColor yellowColor];
    
    if([self.type isEqualToString:@"277"]){
        HomeoneModel *model = self.listArr[indexPath.section];
        NSDictionary *dic = model.allDic.firstObject;
        NSArray *arr = [dic objectForKey:self.type];
        cell.backgroundColor = [UIColor clearColor];
        [cell configUI:arr[indexPath.row]];
    }else{
        
        NSDictionary *model = self.listArr[indexPath.section];
        NSArray *arr = [model objectForKey:@"264"];
        NSDictionary *dic = arr.firstObject;
        NSArray *itemarr = [dic objectForKey:self.type];
        cell.backgroundColor = [UIColor clearColor];
        [cell configUI:itemarr[indexPath.row]];
 
    }
    
    

    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    if([self.type isEqualToString:@"277"]){
        HomeoneModel *model = self.listArr[indexPath.section];
        NSDictionary *dic = model.allDic.firstObject;
        NSArray *arr = [dic objectForKey:self.type];
        TMDetailsVc *VC = [TMDetailsVc new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
        VC.dic = arr[indexPath.row];
    }else{
        
        NSDictionary *model = self.listArr[indexPath.section];
        NSArray *arr = [model objectForKey:@"264"];
        NSDictionary *dic = arr.firstObject;
        NSArray *itemarr = [dic objectForKey:self.type];
        TMDetailsVc *VC = [TMDetailsVc new];
        VC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:VC animated:YES];
        VC.dic = itemarr[indexPath.row];
    }
    
    
    
    
    

}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;{
    return CGSizeMake((kScreenW-65)/2, 250);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section;{
    return  UIEdgeInsetsMake(10, 25, 10, 25);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;{
    return 8;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    

       if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
   
           TMCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"TMSectionView" forIndexPath:indexPath];


           
           if([self.type isEqualToString:@"277"]){
               HomeoneModel *model = self.listArr[indexPath.section];
               if(model.allDic.count > 0){
                   NSDictionary *dic = model.allDic.firstObject;
                   NSArray *arr = [dic objectForKey:self.type];
                   if (arr.count > 0) {
                       view.titleLabel.text = [dic objectForKey:@"name"];
                   }else{
                       view.titleLabel.text = @"";
                   }
                
                   
               }else{
                   view.titleLabel.text = @"";
               }
               
           }else{
               NSDictionary *model = self.listArr[indexPath.section];
               NSArray *arr = [model objectForKey:@"264"];
               
               if(arr.count > 0){
                   NSDictionary *dic = arr.firstObject;
                   view.titleLabel.text = [dic objectForKey:@"name"];
               }else{
                   view.titleLabel.text = @"";
               }
           }
   
           return view;
        } else {
            
            
            UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"TMfootView" forIndexPath:indexPath];
            
            // 设置Footer颜色及样式
            view.backgroundColor = [UIColor clearColor];
            return view;
        }

     

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section;{
    
    
    if([self.type isEqualToString:@"277"]){
        HomeoneModel *model = self.listArr[section];
        if(model.allDic.count > 0){
            NSDictionary *dic = model.allDic.firstObject;
            NSArray *arr = [dic objectForKey:self.type];
            if (arr.count > 0) {
                return CGSizeMake(kScreenW, 30);
            }else{
                return CGSizeMake(kScreenW, 0.1);
            }
         
            
        }else{
            return CGSizeMake(kScreenW, 0.1);
        }
    }else{
        NSDictionary *model = self.listArr[section];
        NSArray *arr = [model objectForKey:@"264"];
        
        if(arr.count > 0){
            return CGSizeMake(kScreenW, 30);
        }else{
            return CGSizeMake(kScreenW, 0.1);
        }
    }
    

    
    
    
   
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section;{
    return CGSizeMake(kScreenW, 0.1);
}


#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return self.mainCollectionView;
}
//
- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self.view;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
}

@end
